package com.qitianyong.adapter;

import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.chrisbanes.photoview.PhotoView;
import com.gkusbsdk.GKUSBFileInfo;
import com.qitianyong.selfclass.CONSTANT;
import com.qitianyong.selfclass.CustomApp;
import com.qitianyong.selfclass.GKDevice;
import com.qitianyong.sgkusbcamera.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gy on 2017/4/6.
 */

public class ViewPicViewPagerAdapter extends PagerAdapter {
    public ArrayList<GKUSBFileInfo> pics = new ArrayList<>();
    private ViewPager viewPager;

    public ViewPicViewPagerAdapter(ViewPager viewPager) {

        this.viewPager = viewPager;
    }

    public void setPics(List objects, int position){
        pics.clear();
        if(objects == null){
            return;
        }
        int currentPosition = 0;
        for (int i = 0; i < objects.size(); i++) {
            Object o = objects.get(i);
            if(o instanceof GKUSBFileInfo){
                pics.add((GKUSBFileInfo) o);
                if(position == i){
                    currentPosition = pics.size() -1;
                }
            }
        }
        notifyDataSetChanged();
        viewPager.setCurrentItem(currentPosition,false);

    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        return pics.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        GKUSBFileInfo gkusbFileInfo = pics.get(position);
        View inflate = View.inflate(container.getContext(), R.layout.module_playphoto, null);
        TextView tv_photoname = (TextView) inflate.findViewById(R.id.tv_photoname);
        PhotoView iv_play_photo = (PhotoView) inflate.findViewById(R.id.iv_play_photo);
        String name = new String(gkusbFileInfo.__name);
        tv_photoname.setText(name);
        DisplayMetrics displayMetrics = container.getContext().getResources().getDisplayMetrics();
        Bitmap asBitmap = CustomApp.__lruCacheHelper.getAsBitmap(name,displayMetrics.widthPixels, displayMetrics.heightPixels);
        iv_play_photo.setTag(name);
        if(asBitmap == null){
            GKDevice.getInstance().sendCommandAddToFirstNoRepeated(CONSTANT.COMMAND.CMD_GET_PHOTO,gkusbFileInfo);
            iv_play_photo.setImageResource(R.mipmap.img_video_list_big_bg);
        }else {
            iv_play_photo.setImageBitmap(asBitmap);
        }
        container.addView(inflate);
        return inflate;
    }

}
